import { Component } from '@angular/core';

import { Util } from '../../utils/util';

import { Subscription } from 'rxjs';

import { ShopComponentService } from './shop.component.service';


import { ShopBase } from './shop.base';

@Component({
  selector: 'app-shop',
  templateUrl: './shop.component.html',
  styleUrls: ['./shop.component.css'],
  providers:[ShopComponentService]
})
export class ShopComponent extends ShopBase {

    // cat option
  option:any = {
    defaultText:true,
    noLeaf:true
  };


  // 参数
  param:any = {
    shopName:'',
    nick:'',
    mainCat:'',
    start:0,
    rows:10,
    sort:'sellMoney',
    sortType:'desc',
    shopType:''
  };

  constructor(public util :Util,public serivce:ShopComponentService) {

    super(util,serivce);

    // 有保存的历史
    if(this.util.sessionStore().has('shop')){

      Object.assign(this.param,this.util.sessionStore().get('shop'));

      this.option.cat = this.param.mainCat;

      this.getHttp(this.param);
    }
  }

  ngOnDestroy(){
    this.util.sessionStore().set('shop',this.param);
    if(this.busy){
      this.busy.unsubscribe();
    }

  }



  // 店铺搜索切换
  searchType(type){


    if(type === "nick"){
      this.param.shopName = "";

    }else{
      this.param.nick = "";

    }

  }

  searchParam:any = {};

  // 店铺搜索查询
  search(){

    // console.log(this.param.nick);

    // console.log(this.param);

    if(this.util.equals(this.param,this.searchParam)){
      return;
    }

    this.searchParam =  Object.assign({}, this.param);

    this.getHttp(this.param);

  }

  getHttp(param){

    this.busy = this.serivce.getShop(param).subscribe((res:any)=>{

      for(let i in res.data.value){
        let item = res.data.value[i];

        item = this.util.shopLevels(item);
      }

      this.vm.table.shops = res.data.value;

      this.vm.table.count = res.data.count;

    },(err:any)=>{

    });
  }

}
